const app = getApp()
const publicFun = require('/utils/public.js')
import { getClimateActionRank } from '/utils/request'
Page({
  data: {
    currentTabIndex: 0,
    tabList: [
      { id: 0, text: '总榜单', zhouqitype: 0},
      { id: 2, text: '周榜单', zhouqitype: 1},
      { id: 1, text: '月榜单', zhouqitype: 2},
    ],
    pageindex: 1,
    rankList: [],
    zhouqitype: 0,
  },
  onLoad() {
    this.getRankList(this.data.pageindex);
  },
  tabTap(e) {
    let data = e.currentTarget.dataset.item;
    this.setData({ 
      currentTabIndex: data.id,
      rankList: [],
      mineRank: [],
      pageindex: 1,
      zhouqitype: data.zhouqitype
    });
    this.getRankList(this.data.pageindex);
  },
  climateUserList(e) {
    let data = e.currentTarget.dataset;
    my.navigateTo({
      url: '/pages/userCheckInList/userCheckInList?userid='+data.userid+"&zhouqitype="+this.data.zhouqitype
    })
  },
  async getRankList(pageindex) { 
    try{
      var params = {
        userid: app.globalData.userid,
        pageindex: pageindex,
        type: this.data.currentTabIndex
      }
      my.showLoading();
      const res = await getClimateActionRank(params)
      my.hideLoading();
      if(res.S == 1){
        let data = res;
        if(data.L.length > 0) {
          data.L.forEach(item => {
            this.data.rankList.push(item);
          });
        }else{
          publicFun.showToast("已经加载到底啦")
        }
        this.setData({ 
          rankList: this.data.rankList,
          mineRank: data.L_Mine
        })
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
    }catch(e){
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
  // 页面被拉到底部
  onReachBottom() {
    const newPage = this.data.pageindex + 1;
    this.setData({pageindex: newPage});
    this.getRankList(newPage)
  },
});
